namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineFibonacciEllipse : PaintLineBase
    {
        public PaintLineFibonacciEllipse()
        {
            base.LineType = PaintLineType.FibonacciEllipse;
            base.PlName_zh = "斐波那契圆";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                double[] numArray = this.LineParam();
                float num = (float) numArray[0];
                float num2 = (float) numArray[1];
                float num3 = (float) numArray[2];
                float num4 = (float) numArray[3];
                RectangleF ef = new RectangleF(num - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num3 - (base.LineWidth * 2.5f), num4 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint))
                {
                    base.MoveType = 1;
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
                else if (ef2.Contains((PointF) crossHairPoint))
                {
                    base.MoveType = 2;
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
                else
                {
                    base.MoveType = 0;
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                g.SmoothingMode = SmoothingMode.AntiAlias;
                Pen pen = new Pen(curColor);
                Brush brush = new SolidBrush(curColor);
                pen.DashStyle = DashStyle.Dash;
                pen.Width = base.LineWidth;
                double[] numArray = this.LineParam(pList);
                float num = ((float) numArray[0]) - base.CgInstance.LeftYScaleWidth;
                float num2 = (((float) numArray[1]) - div.Rect.Y) - div.TitleHeight;
                float num3 = ((float) numArray[2]) - base.CgInstance.LeftYScaleWidth;
                float num4 = (((float) numArray[3]) - div.Rect.Y) - div.TitleHeight;
                g.DrawLine(pen, num, num2, num3, num4);
                float num5 = (((float) numArray[4]) >= 4f) ? ((float) numArray[4]) : 4f;
                float num6 = (((float) numArray[5]) >= 4f) ? ((float) numArray[5]) : 4f;
                float num7 = (((float) numArray[6]) >= 4f) ? ((float) numArray[6]) : 4f;
                float num8 = (((float) numArray[7]) >= 4f) ? ((float) numArray[7]) : 4f;
                float num9 = (((float) numArray[8]) >= 4f) ? ((float) numArray[8]) : 4f;
                pen.Width = base.LineWidth;
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                else
                {
                    pen.DashStyle = DashStyle.Solid;
                }
                g.DrawEllipse(pen, (float) (num - num5), (float) (num2 - num5), (float) (num5 * 2f), (float) (num5 * 2f));
                g.DrawEllipse(pen, (float) (num - num6), (float) (num2 - num6), (float) (num6 * 2f), (float) (num6 * 2f));
                g.DrawEllipse(pen, (float) (num - num7), (float) (num2 - num7), (float) (num7 * 2f), (float) (num7 * 2f));
                g.DrawEllipse(pen, (float) (num - num8), (float) (num2 - num8), (float) (num8 * 2f), (float) (num8 * 2f));
                g.DrawEllipse(pen, (float) (num - num9), (float) (num2 - num9), (float) (num9 * 2f), (float) (num9 * 2f));
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num3 - (base.LineWidth * 2.5f), num4 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                Font font = new Font("New Times Roman", 10f);
                StringFormat format = new StringFormat();
                SizeF ef = g.MeasureString("23.6%", font, 0x3e8, format);
                g.DrawString("23.6%", font, brush, new PointF(num - ef.Width, (num2 - num5) - ef.Height));
                ef = g.MeasureString("38.2%", font, 0x3e8, format);
                g.DrawString("38.2%", font, brush, new PointF(num - ef.Width, (num2 - num6) - ef.Height));
                ef = g.MeasureString("50.0%", font, 0x3e8, format);
                g.DrawString("50.0%", font, brush, new PointF(num - ef.Width, (num2 - num7) - ef.Height));
                ef = g.MeasureString("61.8%", font, 0x3e8, format);
                g.DrawString("61.8%", font, brush, new PointF(num - ef.Width, (num2 - num8) - ef.Height));
                ef = g.MeasureString("100%", font, 0x3e8, format);
                g.DrawString("100%", font, brush, new PointF(num - ef.Width, (num2 - num9) - ef.Height));
                pen.Dispose();
                format.Dispose();
                brush.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
                double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
                base.PointList.Clear();
                base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                base.PointList.Add(new PaintLineBase.PointValue(1, key, valueByPoint));
                return true;
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                double[] numArray = this.LineParam();
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                float num = (float) numArray[0];
                float num2 = (float) numArray[1];
                float num3 = (float) numArray[2];
                float num4 = (float) numArray[3];
                RectangleF ef = new RectangleF(num - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num3 - (base.LineWidth * 2.5f), num4 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint) || ef2.Contains((PointF) crossHairPoint))
                {
                    return true;
                }
                PointF tf = new PointF(crossHairPoint.X - num, crossHairPoint.Y - num2);
                float num5 = (tf.X * tf.X) + (tf.Y * tf.Y);
                for (int i = 4; i < numArray.Length; i++)
                {
                    float num7 = (float) numArray[i];
                    if (((num5 / (num7 * num7)) >= 0.9) && ((num5 / (num7 * num7)) <= 1.1))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count == 0)
            {
                return null;
            }
            PaintLineBase.PointValue value2 = pList[0];
            int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
            PaintLineBase.PointValue value3 = pList[1];
            int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
            float x = base.CgInstance.GetX(pos);
            float num4 = base.CgInstance.GetX(num2);
            PaintLineBase.PointValue value4 = pList[0];
            float num5 = base.CgInstance.GetY(base.OwnedPanelID, value4.Value, null);
            PaintLineBase.PointValue value5 = pList[1];
            float num6 = base.CgInstance.GetY(base.OwnedPanelID, value5.Value, null);
            double num7 = Math.Sqrt((double) (((num4 - x) * (num4 - x)) + ((num6 - num5) * (num6 - num5))));
            double num8 = num7 * 0.236;
            double num9 = num7 * 0.382;
            double num10 = num7 * 0.5;
            double num11 = num7 * 0.618;
            return new double[] { ((double) x), ((double) num5), ((double) num4), ((double) num6), num8, num9, num10, num11, num7 };
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num3 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num5 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num6 = (num3 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num5 - num4);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num8 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            int num9 = (base.CgInstance.GetRecordByTime(value2.Key) + (num8 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[1];
                            int num10 = (base.CgInstance.GetRecordByTime(value3.Key) + (num8 - indexByPoint)) - 1;
                            if (num9 >= 0)
                            {
                                if (num9 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num9 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num9 = 0;
                            }
                            if (num10 < 0)
                            {
                                num10 = 0;
                            }
                            else if (num10 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num10 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num9][base.CgInstance.CalenderScaleField].ToString(), value4.Value + num6);
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num10][base.CgInstance.CalenderScaleField].ToString(), value5.Value + num6);
                            return;
                        }
                        case 1:
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;

                        case 2:
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;
                    }
                }
            }
        }
    }
}

